<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Generic controller for entity class, CI AR.
 * @author hmQuan
 *
 */
class Comment extends MY_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('t_comment');
	}

	
	public function user_comment(){
		$post = parent::validation_user();

		$gcm_regid = $post[GCM_REGID];
		$user_id = $post[T_USERS][USER_ID];
		$data = array();
		if(!isset($post[PRODUCT_ID])){
			parent::print_result('Missing key or values '. PRODUCT_ID);exit();
		}else{
			$productId = $post[PRODUCT_ID];
		}

		if(!isset($post[COMMENT_CONTENT])){
			parent::print_result('Missing key or value ' . COMMENT_CONTENT);exit();
		}else{
			$commnetContent = $post[COMMENT_CONTENT];
		}

		if(isset($post['time'])){
			$time = $post['time'];
		}else{
			$time = -1;
		}

		if(isset($post['page'])){
			$page = $post['page'];
		}else{
			$page = 0;
		}
		if(isset($post['per_page'])){
			$per_page = $post['per_page'];
		}else{
			$per_page = 0;
		}

		$comment = $this->t_comment->set_data(array(USER_ID => $user_id, PRODUCT_ID=>$productId,COMMENT_CONTENT => $commnetContent), 1);

		if(empty($comment)){
			parent::print_result('Insert failed');
		}else{
			$result = $this->t_comment->get_all_commenting($productId, $time);
			// push to own product
			$this->load->model('t_product');
			$product = $this->t_product->get_data_by_id($productId);
			if(!empty($product)){
				parent::push_notification_to_client_via_user_id($product[0][PRODUCT_USER_ID], $post[T_USERS][USER_NAME] . PUSH_MSG_USER_COMMENT, PUSH_COMMENT_PRODUCT);
			}

			foreach ($result as $key => $value) {
				parent::push_notification_to_client_via_user_id($value[COMMENT_USER_ID], $post[T_USERS][USER_NAME] . PUSH_MSG_USER_COMMENT_FOLLOW , PUSH_COMMENT_PRODUCT);
			}
			parent::print_result($result, SUCCESS_VALUE);
		}
		exit();
	}

	/**
	 * [get_comment_all description]
	 * @return [type] [description]
	 */
	public function get_comment_all(){
		$post= $this->input->post();
		$post = parent::check_post_value($post);
		// var_dump($post);
		// check is activity gcm ?
		$gcm_id = $post[GCM_REGID];

		if(!isset($post[COMMENT_PRO_ID]) || empty($post[COMMENT_PRO_ID])){
			parent::print_result('Missing key or value ' . COMMENT_PRO_ID);
			exit();
		}else{
			$productId = $post[COMMENT_PRO_ID];
		}

		if(isset($post['all'])){
			$all = 1;
		}else{
			$all = 0;
		}

		if(isset($post['page'])){
			$page = $post['page'];
		}else{
			$page = 0;
		}
		if(isset($post['per_page'])){
			$per_page = $post['per_page'];
		}else{
			$per_page = 50;
		}

		$result = $this->t_comment->get_all_comment(array(COMMENT_PRO_ID => $post[COMMENT_PRO_ID]), $page, $per_page, $all);
		if(empty($result)){
			parent::print_result('No comment', SUCCESS_VALUE);exit();
		}else{
			parent::print_result($result, SUCCESS_VALUE);exit();
		}
	}



	/**
	 * [get_comment_total_rows description]
	 * @return [type] [description]
	 */
	public function get_comment_total_rows(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);

		// check is activity gcm
		$gcm_regid = $post[GCM_REGID];

		if(!isset($post[COMMENT_PRO_ID]) || empty($post[COMMENT_PRO_ID])){
			parent::print_result(array(T_COMMENT_ST => INVALID),FAILVALUE);
			exit();
		}

		$result = $this->t_comment->get_data_by_property(array(COMMENT_PRO_ID => $post[COMMENT_PRO_ID]));
		parent::print_result(count($result), SUCCESS_VALUE);
		exit();
	}

	public function get_comment_lastest(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);
		if(!isset($post[COMMENT_PRO_ID]) || empty($post[COMMENT_PRO_ID])){
			parent::print_result(array(COMMENT_PRO_ID => INVALID),FAILVALUE);
			exit();
		}else{
			$product_id = $post[COMMENT_PRO_ID];
		}
		$lastest = $this->t_comment->get_lastest_comment($product_id);
		if(empty($lastest)){
			parent::print_result(0);
		}else{
			parent::print_result($lastest, SUCCESS_VALUE);
		}
		exit();
	}
}